#!/usr/bin/env python3

##    MIT License
##
##    Copyright (c) 2025 and later AJ_Lethal
##
##    Permission is hereby granted, free of charge, to any person obtaining a copy
##    of this software and associated documentation files (the "Software"), to deal
##    in the Software without restriction, including without limitation the rights
##    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
##    copies of the Software, and to permit persons to whom the Software is
##    furnished to do so, subject to the following conditions:
##
##    The above copyright notice and this permission notice shall be included in all
##    copies or substantial portions of the Software.
##
##    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
##    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
##    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
##    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
##    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
##    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
##    SOFTWARE.

import tkinter as tk
from tkinter import ttk
from tkinter import simpledialog
from tkinter import filedialog
from tkinter import messagebox
from tkinter import StringVar
from idlelib.tooltip import Hovertip
import json
import os
import io
import binascii
import struct
import hashlib

## check for unsaved changes upon being called on opening
def unsavedChanges():
    if dirtyFlag == 1:
        confirmChanges = messagebox.askyesnocancel("Confirm exit", "You have unsaved changes, do you want to save before exiting?")
        if confirmChanges is None:
            return
        elif confirmChanges:
            saveProfile()
            root.destroy()
        else:
            root.destroy()
    else:
        root.destroy()

## "About" dialog
def aboutDlg(*args):
    aboutMsg = messagebox.showinfo("About MemphisRider",
                                   "MemphisRider v1.5 \n"
                                   "A tool to manage and export NFSU2 profile garages' data\n"
                                    "(c) 2025 and later AJ_Lethal\n\n"
                                    "Licensed under the MIT License")
## icon data
sIconData = b'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADG0l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vV70Gwa/lwvmq7NsdrYSJjpSIxELMFEeApNV6msLaO0sojyZaV0d/UEgFoVmAZ6x8cm79/3pwNi8+Ot+HO9s4d1d10kkO8j2+8hFktw5kQ3Q30jRCYiRCanEUKw6wfbKSjOvWm1qWle/eNbhAbHcLodOLMceAMe8gpzCOT5qKgtFd1dPQrQINK+KEvXgR2qpgi70z6HgI2ra2l/fieKqmAaJoZxw+dWmneariIz3GdZFol48kaXIkBKgZQSRVWwLIuj73Ty8q9eMYAfqgCdHcHe5tam54HBVNKomRqPZHpho2VZ9psMl3OULdhqC4HNri+6bhrmrU1pGsRzgJaxdwOwSUq5aIL6wmIx00kpQMWcEtbZETSBeEaJBiCnwI/4HyEQUlC4pGDhtnwxuXyhj2gkhtvjXHA9EU/OXFxmxWbT5za1s5xKcfnC1dtfzTIsUA68B5StWNfAhq0t+AIehJRYaQamkine2XeY6yPjCCmwTAtFVdj8+AP40tEkhMCyLKJT0xx/9yTHDp1iOhKbBNrvaNfm1qZa4ABQdnvPEk7XegH47mDdSaAdePVzOTYNoh2omtdHWuknCvwCGEun9m8D1WkwmTpM4Fr63vlhZ0cwyf9b/guNuhuUyutx2QAAAABJRU5ErkJggg=='

## button icon data, icons based on Tango Desktop set
openBtnIconData = b'iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAA3WAAAN1gGQb3mcAAAD0ElEQVRIx6WVS2tdVRTHf3vvcx9NbrANgkI7SgLFWsWUDgwU4geooyY4FIuIH8CB4sB+BRUc9AOI0IHFgQOhLQFrrXYifVCtVGNI2t60zc19nHPu2Xuv5eA+cpNg29g/LNicc9b67b0e+xieosV3Tp2x1nwCxqqKMcb03/QW5VLprkp8t9uVa+fOnSt2+huAxcVFNzU1Vcvz3Oz84EH93qcnTpz46PR772OM2WYA589/y4VLF9aajcbpiYn9F8+ePetH/ROAmZmZX40x07VabdcJsvyFSqeTsrKygvc9X1UlxoiqcvLk2xRF8fKVq1e+3Gg0Pp6fn/9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T8Osn+gGyEEQojcnGUilOXg0tJSW6Xl1vPe/i6uZCfZ2ryDE2ND7Dvazcvbu221Qogc1TmtCIfDBcd8cmaAef07HgzU8u3sF/hrGxka76ftjRHcsxsB6Dt4YGUwQHv7zhzooS9DTF/4muaGR5mIDpLWE/wYPcHDdY8wODJA+s61lMRrcr4wr8dLBSTx+ALx+AKfhg/z8dirNNU/xrnYMGk9AUBaT3AuNszmQDMJ5wQJ33xej2Wh6Xu9PiYunKFn5CWa6pq4zVfG5g07EBKkIlA0QWPN01SUV1Lvr+eKY5i9oTdtcQVbEQqFAOg9vwuAr0Y/B+CZp17AWawihImUAlUTBI/02KDR6AdsnWrFX1KPaZq54JaWFl7s6ALgbp63X4q4j5DSE7iKNQwyOBwODJEFYKDjMl6vj1AoxMb19zIfm+OH6GguWAiBs6gob1eocg2pzHVcLg1dGDf29RLY7fFgmoY9p1Nnx5hNnM8HK6qWB3art3MteRXvuhIWs1GEFOhFV3GqHl5/t4Mqb7V9MnuOd5LKLvwLtuBvv/aKPWEr7vlohouXvmfT+mqmrv+FdAgW+ZP77q/hmzP9bGn8BE3V2PV+G9cyv1HqrEUBDED6fL4V78HdbV3s/HATTY0P4VInSRl/M5f8mQcCW0jrGTo/2w4meLxOQNBQ3mqI3t7e/3Qfh6cOYXgu0rrtCSKpUyxmLuMQKncV17JOq+LX339h8NhJqt2P469oDgnrGIql/abccBXQlrnzniflBl+VPKypzrWBgF9WlFewxuthJjLHT5NnjenpS8I0zf3j+zK7AcT//ZnW7VHfQ9AOuDCRQBI4DXSOv5MZsvL+AWGthETiHqtEAAAAAElFTkSuQmCC'

## defines main window, frames and their weights
root = tk.Tk()
root.title("MemphisRider for NFSU2")
root.protocol('WM_DELETE_WINDOW', unsavedChanges)
root.minsize(300,450)
smallIcon = tk.PhotoImage(data=sIconData)
largeIcon = tk.PhotoImage(data=lIconData)
root.iconphoto(False, largeIcon, smallIcon)

mainFrame = ttk.Frame(root, padding=(5,10,5,10))
mainFrame.grid(row = 0, column = 0, sticky="NSEW")
topFrame = ttk.Frame(mainFrame, padding=(5,0,5,10))
topFrame.grid(row = 0, column = 0, sticky="NSEW")
mainNotebook = ttk.Notebook(mainFrame)
mainNotebook.grid(row = 1, column = 0, sticky='NSEW')
myCarsTab = ttk.Frame(mainNotebook, padding=(5,10,5,10))
myCarsTab.grid(row = 0, column = 0, sticky='NSEW')
myCarsTabLeft = ttk.Frame(myCarsTab, padding=(5,10,5,10))
myCarsTabLeft.grid(row = 0, column = 0, sticky='NSEW')
myCarsTabRight = ttk.Frame(myCarsTab, padding=(5,10,5,10))
myCarsTabRight.grid(row = 0, column = 2, sticky='NSEW')
careerTab = ttk.Frame(mainNotebook, padding=(5,10,5,10))
careerTab.grid(row = 0, column = 0, sticky='NSEW')
careerTabLeft = ttk.Frame(careerTab, padding=(5,10,5,10))
careerTabLeft.grid(row = 0, column = 0, sticky='NSEW')
careerTabRight = ttk.Frame(careerTab, padding=(5,10,5,10))
careerTabRight.grid(row = 0, column = 2, sticky='NSEW')
bottomFrame = ttk.Frame(mainFrame, padding=(0,5,0,0))
bottomFrame.grid(row = 2, column = 0, sticky="NSEW")

root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
mainFrame.columnconfigure(0, weight=1)
mainFrame.rowconfigure(1, weight=1)
topFrame.columnconfigure(0, weight=0)
topFrame.columnconfigure(1, weight=0)
topFrame.columnconfigure(2, weight=0)
topFrame.columnconfigure(3, weight=1)
topFrame.rowconfigure(0, weight=0)
mainNotebook.columnconfigure(0, weight=1)
mainNotebook.rowconfigure(0, weight=1)
myCarsTab.columnconfigure(0, weight=1)
myCarsTab.rowconfigure(0, weight=1)
myCarsTabLeft.columnconfigure(0, weight=1)
myCarsTabLeft.rowconfigure(0, weight=1)
myCarsTabRight.columnconfigure(0, weight=1)
careerTab.columnconfigure(0, weight=1)
careerTab.rowconfigure(0, weight=1)
careerTabLeft.columnconfigure(0, weight=1)
careerTabLeft.rowconfigure(0, weight=1)
careerTabRight.columnconfigure(0, weight=1)
bottomFrame.columnconfigure(0, weight=1)

## adds tabs to mainNotebook
mainNotebook.add(myCarsTab, text = "My Cars Garage")
mainNotebook.add(careerTab, text = "Career Garage")

## loads button icon data for use
openBtnIcon = tk.PhotoImage(data=openBtnIconData)
saveBtnIcon = tk.PhotoImage(data=saveBtnIconData)
saveAsBtnIcon = tk.PhotoImage(data=saveAsBtnIconData)
reloadBtnIcon = tk.PhotoImage(data=reloadBtnIconData)
toolsBtnIcon = tk.PhotoImage(data=toolsBtnIconData)
addXnameBtnIcon = tk.PhotoImage(data=addXnameBtnIconData)
chgXnameBtnIcon = tk.PhotoImage(data=chgXnameBtnIconData)
aboutBtnIcon = tk.PhotoImage(data=aboutBtnIconData)
slotExportIcon = tk.PhotoImage(data=slotExportIconData)
slotImportIcon = tk.PhotoImage(data=slotImportIconData)
slotClearIcon = tk.PhotoImage(data=slotClearIconData)
moveSlotUpIcon = tk.PhotoImage(data=moveSlotUpIconData)
moveSlotDownIcon = tk.PhotoImage(data=moveSlotDownIconData)
exportPresetIcon = tk.PhotoImage(data=exportPresetIconData)
importPresetIcon = tk.PhotoImage(data=importPresetIconData)

## defines tab button style
tabButton = ttk.Style()
tabButton.configure('TabButton.TButton', anchor='w')

## initializing main variables
dirtyFlag = 0

xnames = {
            "PEUGOT":"0x13E5B272",
            "FOCUS": "0xDF04CA02",
            "COROLLA":"0xEB137CF7",
            "240SX":"0x80FB5001",
            "MIATA":"0x6B5D4503",
            "CIVIC":"0x4DC09002",
            "PEUGOT106":"0x0A07104B",
            "CORSA":"0xD7FA9302",
            "HUMMER":"0x4DDD2661",
            "NAVIGATOR":"0x4AC91902",
            "ESCALADE":"0x714770CF",
            "TIBURON":"0x2205FD04",
            "SENTRA":"0xECBFAE79",
            "CELICA":"0x60EC5A54",
            "IS300":"0xEE360203",
            "SUPRA":"0x8AC4B803",
            "GOLF":"0x87301600",
            "A3":"0x53040000",
            "RSX":"0x7CDB0000",
            "ECLIPSE":"0x84DA0275",
            "TT":"0xE7060000",
            "RX8":"0x01DC0000",
            "350Z":"0xD1C60A00",
            "G35":"0x6EA80000",
            "3000GT":"0x5D9B7C2D",
            "GTO":"0xC9AC0000",
            "MUSTANGGT":"0x19581635",
            "SKYLINE":"0x5E3748BE",
            "LANCEREVO8":"0xF6EFD209",
            "RX7":"0x00DC0000",
            "IMPREZAWRX":"0xF85844CF",
            "(empty)": "0x00000000"
          }

userXnames = {}

userDirPaths = {
            "openProfileDir":"",
            "importPresetDir":"",
            "exportPresetDir":"",
            "importSlotDir":"",
            "exportSlotDir":"",
            }

openProfilePath = ''
openProfilePathPrev = ''

myCarsSlotsList = []
myCarsSlotsListVar = tk.StringVar()
selectedMyCarsSlot = 0
firstFoundMyCarsID = 0

careerSlotsList = []
careerSlotsListVar = tk.StringVar()
selectedCareerSlot = 0
firstFoundCareerID = 0

newXname = tk.StringVar()
newXnameHash = tk.StringVar()

filePathStr = tk.StringVar()

activeList = 0

presetImportFlag = 0

carSlots = []
selSlot = 0

myCarsSlots = []
careerSlots = []

importPerfLvCnc = 0

slotsPresetNames = {}

reloadFlag = False

## function to clear slot lists upon being called
def clearCarSlots():
    global myCarsSlots
    global careerSlots
    
    myCarsSlots.clear()
    for i in range(20):
        myCarsSlots.append(list())
        myCarsSlots[i].append(0)
        myCarsSlots[i].append(0)
        myCarsSlots[i].append(0)

    careerSlots.clear()
    for i in range(20):
        careerSlots.append(list())
        careerSlots[i].append(0)
        careerSlots[i].append(0)
        careerSlots[i].append(0)
        careerSlots[i].append(0)
        
## function to load user XNAMES from a file
def loadUserXnames():
    global userXnames

    if os.path.isfile("userXnames.txt") == True:
        with open ("userXnames.txt", 'r') as userXnamesFile:
            userXnames = json.load(userXnamesFile, parse_int=True)
            userXnamesFile.close()
    else:
        userXnames.clear()

## function to load list of recent folders for profiles, presets and slots from a file
def loadUserDirPaths():
    global userDirPaths
    
    if os.path.isfile("userDirPaths.txt") == True:
        with open ("userDirPaths.txt", 'r') as userDirPathsFile:
            userDirPaths = json.load(userDirPathsFile, parse_int=True)
            userDirPathsFile.close()
    else:
        pass
    
## writes list of recent folders for profiles, presets and slots to a file
def saveUserDirPaths():
    global userDirPaths
    
    with open ("userDirPaths.txt", 'w') as userDirPathsFile:
        userDirPathsFile.write(json.dumps(userDirPaths, indent=4))
        userDirPathsFile.close


## function to load list of recent folders for profiles, presets and slots from a file
def loadSlotPresetNames():
    global slotsPresetNames
    
    if os.path.isfile("slotPresetNames.txt") == True:
        with open ("slotPresetNames.txt", 'r') as slotPresetNamesFile:
            slotsPresetNames = json.load(slotPresetNamesFile, parse_int=True)
            slotPresetNamesFile.close()
    else:
        pass
    
## generates MD5 hash of imported preset/slot customization data and checks against slotPresetNames; if hash does not exist, it will be added alongside the preset/slot file name to it.
def slotPresetNameHash(presetData, filePath):
    global slotPresetNames
    presetDataHash = hashlib.md5(presetData).hexdigest()
    presetName = os.path.splitext(os.path.split(filePath)[1])[0]
    
    slotsPresetNames[presetDataHash] = presetName

    saveSlotPresetNames()

## writes MD5 hash of preset to a file
def saveSlotPresetNames():
    global slotsPresetNames
    
    with open ("slotPresetNames.txt", 'w') as slotPresetNamesFile:
        slotPresetNamesFile.write(json.dumps(slotsPresetNames, indent=4))
        slotPresetNamesFile.close

    loadSlotPresetNames()

loadUserXnames()
loadUserDirPaths()

## functions to validate characters inputted in text boxes
def inputCallback(string, newString):
    spChars = ['', '_']
    if len(newString) > 32:
            return False
    if len(string) > 32:
            return False

    for i in str(string):
        if i.isalnum():
            return True
        elif i in spChars:
            return True
        else:
            return False

def inputCallbackHex(string, newString):
    acceptedChars = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','A','B','C','D','E','F','x','']
    if len(newString) > 10:
            return False
    if len(string) > 10:
            return False
        
    for i in str(string):
        if not i in acceptedChars:
            return False
        else:
            return True

## sets fileLabel to active file
def openFileLabel():
    fileLabel['textvariable'] = filePathStr
    if len(f'File: {openProfilePath}') < 72:
        filePathStr.set(f'File: {openProfilePath}')
    else:
        longpath1= os.path.splitdrive(openProfilePath)
        longpath2= os.path.split(longpath1[1])
        longpath3= os.path.split(longpath2[0])
        longpath4= os.path.split(longpath3[0])
        filePathStr.set(f'File: {longpath1[0]}\\...\\{longpath4[1]}\\{longpath3[1]}\\{longpath2[1]}')

## checks XNAME hashes passed through it against the built-in XNAME list and the user XNAME list, then returns the result
def checkSlotXname(slot):
    slotXnameHashTmp = binascii.hexlify(slot)
    slotXnameCheck = str('0x' + slotXnameHashTmp.decode().upper())
    if slotXnameCheck in xnames.values() or slotXnameCheck in userXnames.values():
        for key, value in xnames.items():
            if value == slotXnameCheck:
                return key
            else:
                for key, value in userXnames.items():
                    if value == slotXnameCheck:
                        return key                    
    else:
        return slotXnameCheck

## sets active tab to determine operations on My Cars or Career slots
def activeTab(*args):
    global activeList
    activeList = mainNotebook.index(mainNotebook.select()) + 1
    
## populates car lists from profile
def myCarsListboxPopulate():
    myCarsSlotsList.clear()
    for i in range(20):
        if openProfilePath:
            myCarsSlots[i][0] = checkSlotXname(myCarsSlots[i][1])
            slotPresetName = hashlib.md5(myCarsSlots[i][2][28:776]).hexdigest()
        if slotPresetName in slotsPresetNames.keys():
            myCarsSlotsList.append(f"{myCarsSlots[i][0]} ({slotsPresetNames[slotPresetName]})")
        else:
            myCarsSlotsList.append(f"{myCarsSlots[i][0]}")
    myCarsSlotsListVar.set(myCarsSlotsList)

def careerListboxPopulate():
    careerSlotsList.clear()
    for i in range(5):
        if openProfilePath:
            careerSlots[i][0] = checkSlotXname(careerSlots[i][1])
            slotPresetName = hashlib.md5(careerSlots[i][2][28:776]).hexdigest()
        if slotPresetName in slotsPresetNames.keys():
            careerSlotsList.append(f"{careerSlots[i][0]} ({slotsPresetNames[slotPresetName]})")
        else:
            careerSlotsList.append(f"{careerSlots[i][0]}")
    careerSlotsListVar.set(careerSlotsList)

## sets loaded slot index, also enables or disables UI elements depending of slot
def loadSlots(*args):
    global selectedMyCarsSlot
    global selectedCareerSlot
    careerSlotCount = 0
    if activeList == 1:
        selectedMyCarsSlotInput = myCarsListbox.curselection()
        if len(selectedMyCarsSlotInput)==1:
           selectedMyCarsSlot = int(selectedMyCarsSlotInput[0])
        if myCarsSlots[selectedMyCarsSlot][0] == "(empty)":
            clearMyCarsSlotBtn.state(['disabled'])
            exportMyCarsPsetBtn.state(['disabled'])
            exportMyCarsSlotBtn.state(['disabled'])
            myCarsMoveSlotUpBtn.state(['disabled'])
            myCarsMoveSlotDownBtn.state(['disabled'])
        else:
            clearMyCarsSlotBtn.state(['!disabled'])
            exportMyCarsPsetBtn.state(['!disabled'])
            exportMyCarsSlotBtn.state(['!disabled'])
            myCarsMoveSlotUpBtn.state(['!disabled'])
            myCarsMoveSlotDownBtn.state(['!disabled'])

        if selectedMyCarsSlot == 0 and myCarsSlots[selectedMyCarsSlot][0] != '(empty)':
            myCarsMoveSlotUpBtn.state(['disabled'])
            myCarsMoveSlotDownBtn.state(['!disabled'])
        if selectedMyCarsSlot == 19 and myCarsSlots[selectedMyCarsSlot][0] != '(empty)':
            myCarsMoveSlotUpBtn.state(['!disabled'])
            myCarsMoveSlotDownBtn.state(['disabled'])            
        if selectedMyCarsSlot != 0 and selectedMyCarsSlot != 19 and myCarsSlots[selectedMyCarsSlot][0] != '(empty)':
            myCarsMoveSlotUpBtn.state(['!disabled'])
            myCarsMoveSlotDownBtn.state(['!disabled'])
            
    if activeList == 2:
        selectedCareerSlotInput = careerListbox.curselection()
        if len(selectedCareerSlotInput)==1:
           selectedCareerSlot = int(selectedCareerSlotInput[0])
        for i in range(5):
            if careerSlots[i][2][1069:1070] == b'\x00':
                pass
            else:
                careerSlotCount = careerSlotCount+1
        if careerSlots[selectedCareerSlot][0] == "(empty)":
            clearCareerSlotBtn.state(['disabled'])
            exportCareerPsetBtn.state(['disabled'])
            exportCareerSlotBtn.state(['disabled'])
            careerMoveSlotUpBtn.state(['disabled'])
            careerMoveSlotDownBtn.state(['disabled'])
        else:
            clearCareerSlotBtn.state(['!disabled'])
            exportCareerPsetBtn.state(['!disabled'])
            exportCareerSlotBtn.state(['!disabled'])
        if careerSlotCount == 1:
            clearCareerSlotBtn.state(['disabled'])

        if selectedCareerSlot == 0 and careerSlots[selectedCareerSlot][0] != '(empty)':
            careerMoveSlotUpBtn.state(['disabled'])
            careerMoveSlotDownBtn.state(['disabled'])
            clearCareerSlotBtn.state(['disabled'])
        if selectedCareerSlot == 4 and careerSlots[selectedCareerSlot][0] != '(empty)':
            careerMoveSlotUpBtn.state(['!disabled'])
            careerMoveSlotDownBtn.state(['disabled'])
        if selectedCareerSlot != 0 and selectedCareerSlot != 4 and careerSlots[selectedCareerSlot][0] != '(empty)':
            careerMoveSlotUpBtn.state(['!disabled'])
            careerMoveSlotDownBtn.state(['!disabled'])

## opens profile file, then loads slot data and enables UI elements
def openProfile(*args):
    global dirtyFlag
    global openProfilePath
    global openProfilePathPrev
    global openProfileDir

    loadSlotPresetNames()

    if dirtyFlag == 1:
        confirmChanges = messagebox.askyesnocancel("Confirm changes", "You have unsaved changes, do you want to save?")
        if confirmChanges is None:
            return
        elif confirmChanges:
            saveProfile()
    if openProfilePath != "":
        openProfilePathPrev = openProfilePath
        loadUserXnames()
    if reloadFlag == False:
        openProfilePath = filedialog.askopenfilename(title="Open your NFSU2 save file", initialdir=userDirPaths["openProfileDir"])
    if openProfilePath == "":
        openProfilePath = openProfilePathPrev
        return
    with open (openProfilePath, 'rb') as profile:
        clearCarSlots()
        if len(profile.read()) != 54966:
            badFile=messagebox.showerror(title="Error", message="Invalid profile file, please select another file")
            return
        profile.seek(1196)
        saveProfileBtn.state(['!disabled'])
        saveAsProfileBtn.state(['!disabled'])
        reloadProfileBtn.state(['!disabled'])
        myCarsListbox['state'] = tk.NORMAL
        exportMyCarsSlotBtn.state(['!disabled'])
        importMyCarsSlotBtn.state(['!disabled'])
        clearMyCarsSlotBtn.state(['!disabled'])
        myCarsMoveSlotUpBtn.state(['!disabled'])
        myCarsMoveSlotDownBtn.state(['!disabled'])
        exportMyCarsPsetBtn.state(['!disabled'])
        importMyCarsPsetBtn.state(['!disabled'])
        careerListbox['state'] = tk.NORMAL
        exportCareerSlotBtn.state(['!disabled'])
        importCareerSlotBtn.state(['!disabled'])
        clearCareerSlotBtn.state(['!disabled'])
        careerMoveSlotUpBtn.state(['!disabled'])
        careerMoveSlotDownBtn.state(['!disabled'])
        exportCareerPsetBtn.state(['!disabled'])
        importCareerPsetBtn.state(['!disabled'])
        for i in range (20):
            myCarsSlots[i][2] = profile.read(1072)
            myCarsSlots[i][1] = myCarsSlots[i][2][24:28]
            if struct.unpack('B', myCarsSlots[i][2][8:9])[0] == 1:
                myCarsSlots[i][0] = checkSlotXname(myCarsSlots[i][1])
            else:
                myCarsSlots[i][0] = '(empty)'
        for i in range (5):
            careerSlots[i][2] = profile.read(1072)
            careerSlots[i][3] = profile.read(962)
            careerSlots[i][1] = careerSlots[i][2][24:28]
            if struct.unpack('B', careerSlots[i][2][1069:1070])[0] == 1:
                careerSlots[i][0] = checkSlotXname(careerSlots[i][1])
            else:
                careerSlots[i][0] = '(empty)'
        profile.close()
        userDirPaths["openProfileDir"] = os.path.split(openProfilePath)[0]
        saveUserDirPaths()
        
    openFileLabel()
    dirtyFlag = 0        
    myCarsListboxPopulate()
    careerListboxPopulate()

## saves profile file
def saveProfile(*args):
    global dirtyFlag
    if openProfilePath:
        with open (openProfilePath, 'r+b') as profile:
            profile.seek(1196)
            for i in range (20):
                profile.write(myCarsSlots[i][2])
            for i in range (5):
                profile.write(careerSlots[i][2])
                profile.write(careerSlots[i][3])
            profile.seek(50639)
            for i in range(20):
                if myCarsSlots[i][2][0:4] == b'\x00\x00\x00\x00':
                    pass
                else:
                    firstFoundMyCarsID = myCarsSlots[i][2][0:4]
                    profile.write(firstFoundMyCarsID)
                    break
            if firstFoundMyCarsID == 0:
                profile.write(struct.pack('>I', b'\x16\x1e\x9b\x95'))
            profile.seek(44415)
            for i in range(5):
                if careerSlots[i][2][0:4] == b'\x00\x00\x00\x00':
                    pass
                else:
                    firstFoundCareerID = careerSlots[i][2][0:4]
                    profile.write(firstFoundCareerID)
                    break
            profile.close()  
        dirtyFlag = 0
        if len(f'File saved to: {openProfilePath}') < 72:
            filePathStr.set(f'File saved to: {openProfilePath}')
        else:
            longpath1= os.path.splitdrive(openProfilePath)
            longpath2= os.path.split(longpath1[1])
            longpath3= os.path.split(longpath2[0])
            longpath4= os.path.split(longpath3[0])
            filePathStr.set(f'File saved to: {longpath1[0]}\\...\\{longpath4[1]}\\{longpath3[1]}\\{longpath2[1]}')
        fileLabel.after(5000, openFileLabel)

## saves profile file as another file
def saveProfileAs(*args):
    global dirtyFlag
    global openProfilePath
    global openProfilePathPrev
    global openProfileDir
    openProfilePathPrev = openProfilePath
    if openProfilePath:
        with open (openProfilePath, 'rb') as profile:
            openProfile = profile.read()
            profile.seek(0)
            saveProfilePath = filedialog.asksaveasfilename(title="Save NFSU2 profile as...", filetypes=[("NFSU2 profile", "*.*")])
            if saveProfilePath == "":
                openProfilePath = openProfilePathPrev
                return
            with open (saveProfilePath, 'wb') as profileWrite:
                profileWrite.write(openProfile)
                profileWrite.seek(1196)
                for i in range (20):
                    profileWrite.write(myCarsSlots[i][2])
                for i in range (5):
                    profileWrite.write(careerSlots[i][2])
                    profileWrite.write(careerSlots[i][3])
                profileWrite.seek(50639)
                for i in range(20):
                    if myCarsSlots[i][2][0:4] == b'\x00\x00\x00\x00':
                        pass
                    else:
                        firstFoundMyCarsID = myCarsSlots[i][2][0:4]
                        profileWrite.write(firstFoundMyCarsID)
                        break
                if firstFoundMyCarsID == 0:
                    profileWrite.write(struct.pack('>I', b'\x16\x1e\x9b\x95'))
                profileWrite.seek(44415)
                for i in range(5):
                    if careerSlots[i][2][0:4] == b'\x00\x00\x00\x00':
                        pass
                    else:
                        firstFoundCareerID = careerSlots[i][2][0:4]
                        profileWrite.write(firstFoundCareerID)
                profileWrite.seek(53797)
                profileWrite.write(b'\x00\x00\x00\x00\x00\x00\x00')
                profileWrite.seek(53797)
                profileWrite.write(os.path.split(saveProfilePath)[1][0:7].encode('ascii'))
                profileWrite.close()
                userDirPaths["openProfileDir"] = os.path.split(openProfilePath)[0]
                saveUserDirPaths()
            dirtyFlag = 0
            if len(f'File saved to: {saveProfilePath}') < 72:
                filePathStr.set(f'File saved to: {saveProfilePath}')
            else:
                longpath1= os.path.splitdrive(saveProfilePath)
                longpath2= os.path.split(longpath1[1])
                longpath3= os.path.split(longpath2[0])
                longpath4= os.path.split(longpath3[0])
                filePathStr.set(f'File saved to: {longpath1[0]}\\...\\{longpath4[1]}\\{longpath3[1]}\\{longpath2[1]}')
            fileLabel.after(5000, openFileLabel)

## reloads profile
def reloadProfile(*args):
    global reloadFlag
    reloadFlag = True
    openProfile()
    reloadFlag = False
    
## exports selected slot to a .u2cc file, if it's a career mode slot it will also export part inventory data to a .u2ci file
def exportSlot(*args):
    global myCarsSlots
    global careerSlots
    global exportSlotDir
    if openProfilePath:
        slotSave = filedialog.asksaveasfilename(title="Export car slot", filetypes=[("MemphisRider custom car slot", "*.u2cc")], defaultextension=[".u2cc"], initialdir=userDirPaths["exportSlotDir"])
        if slotSave == "":
            return
        
        if activeList == 1:
            exportSlots = myCarsSlots
            selSlot = selectedMyCarsSlot
        if activeList == 2:
            exportSlots = careerSlots
            selSlot = selectedCareerSlot
        
        with open (slotSave, 'wb') as slotSaveWrite:
            slotSaveWrite.write(exportSlots[selSlot][2])
            slotSaveWrite.close()
            slotPresetNameHash(exportSlots[selSlot][2][28:776], slotSave)
        if activeList == 2:
            slotSaveInv = slotSave.replace(".u2cc", ".u2ci")
            with open (slotSaveInv, 'wb') as slotSaveInvWrite:
                slotSaveInvWrite.write(exportSlots[selSlot][3])
            slotSaveInvWrite.close()
        if len(f'Slot {selSlot+1} exported to: {slotSave}') < 64:
            filePathStr.set(f'Slot {selSlot+1} exported to: {slotSave}')
        else:
            longpath1= os.path.splitdrive(slotSave)
            longpath2= os.path.split(longpath1[1])
            longpath3= os.path.split(longpath2[0])
            longpath4= os.path.split(longpath3[0])
            filePathStr.set(f'Slot {selSlot+1} exported to: {longpath1[0]}\\...\\{longpath3[1]}\\{longpath2[1]}')

        userDirPaths["exportSlotDir"] = os.path.split(slotSave)[0]
        saveUserDirPaths()
        myCarsListboxPopulate()
        careerListboxPopulate()
        loadSlots()
        fileLabel.after(5000, openFileLabel)

## imports car in .u2cc file to selected slot, if it's a career mode slot it will also attempt to import part inventory in .u2ci file;
## if not found it will notify user it will use the part inventory from the slot
def importSlot(*args):
    global dirtyFlag
    global myCarsSlots
    global careerSlots
    global importSlotDir
    if openProfilePath:
        slotOpen = filedialog.askopenfilename(title="Import slot to My Cars", filetypes=[("MemphisRider custom car slot", "*.u2cc")], initialdir=userDirPaths["importSlotDir"])
        if slotOpen == "":
            return
        
        if activeList == 1:
            importSlots = myCarsSlots
            selSlot = selectedMyCarsSlot
        if activeList == 2:
            importSlots = careerSlots
            selSlot = selectedCareerSlot
            
        with open (slotOpen, 'rb') as slotOpenRead:
            importSlots[selSlot][2] = slotOpenRead.read()
            slotOpenRead.close()
            importSlots[selSlot][1] = importSlots[selSlot][2][24:28]
            importSlots[selSlot][0] = checkSlotXname(importSlots[selSlot][1])
            importSlots[selSlot][2] = bytearray(importSlots[selSlot][2])
            slotPresetNameHash(importSlots[selSlot][2][28:776], slotOpen)
            if activeList == 1:
                if selSlot < 9:
                    importSlots[selSlot][2][0:4] = f"0{selSlot+1}MC".encode('ascii')
                    importSlots[selSlot][2][4:8] = f"MC0{selSlot+1}".encode('ascii')
                else:
                    importSlots[selSlot][2][0:4] = f"{selSlot+1}MC".encode('ascii')
                    importSlots[selSlot][2][4:8] = f"MC{selSlot+1}".encode('ascii')
                importSlots[selSlot][2][8:9] = struct.pack('>B',1)
                importSlots[selSlot][2][12:13] = struct.pack('>B',2)
            if activeList == 2:
                importSlots[selSlot][2][0:4] = f"0{selSlot+1}CR".encode('ascii')
                importSlots[selSlot][2][4:8] = f"CR0{selSlot+1}".encode('ascii')
                importSlots[selSlot][2][8:9] = struct.pack('>B',1)
                importSlots[selSlot][2][12:13] = struct.pack('>B',4)
                importSlots[selSlot][2][1069:1070] = struct.pack('>B',1)
                slotOpenInv = slotOpen.replace(".u2cc",".u2ci")
                if os.path.isfile(slotOpenInv) == True:
                    with open (slotOpenInv, 'rb') as slotOpenInvRead:
                        importSlots[selSlot][3] = slotOpenInvRead.read()
                        slotOpenInvRead.close()
                else:
                    tk.messagebox.showinfo(title="Attention", message="No part inventory file (*.u2ci) found for this slot. \nImported slot will inherit the inventory from the save file slot.")
            userDirPaths["importSlotDir"] = os.path.split(slotOpen)[0]
            saveUserDirPaths()
            
        importSlots[selSlot][2] = bytes(importSlots[selSlot][2])
        myCarsListboxPopulate()
        careerListboxPopulate()
        loadSlots()
        
        if len(f'Imported {slotOpen} to slot {selSlot+1}') < 72:
            filePathStr.set(f'Imported {slotOpen} to slot {selSlot+1}')
        else:
            longpath1= os.path.splitdrive(slotOpen)
            longpath2= os.path.split(longpath1[1])
            longpath3= os.path.split(longpath2[0])
            longpath4= os.path.split(longpath3[0])
            filePathStr.set(f'Imported {longpath1[0]}\\...\\{longpath3[1]}\\{longpath2[1]} to slot {selSlot+1}')
        fileLabel.after(5000, openFileLabel)
        dirtyFlag = 1

## clears slot data, setting up default data to make slot usable again
def clearSlot(*args):
    global dirtyFlag
    if openProfilePath:
        if activeList == 1:
            carSlots = myCarsSlots
            selSlot = selectedMyCarsSlot
        if activeList == 2:
            carSlots = careerSlots
            selSlot = selectedCareerSlot
        
        carSlots[selSlot][2] = bytearray(b'\x00'*1072)
        carSlots[selSlot][2][8:9] = struct.pack('>B',1)
        carSlots[selSlot][2][856:1061] = bytearray(b'\x64'*205)
        carSlots[selSlot][1] = carSlots[selSlot][2][24:28]
        carSlots[selSlot][0] = '(empty)'
        if activeList == 1:
            carSlots[selSlot][2][12:13] = struct.pack('>B',2)
        if activeList == 2:
            carSlots[selSlot][2][12:13] = struct.pack('>B',4)    
        carSlots[selSlot][2] = bytes(carSlots[selSlot][2])    
        myCarsListboxPopulate()
        careerListboxPopulate()
        loadSlots()
        dirtyFlag = 1

## moves slot up
def moveSlotUp(*args):
    global myCarsSlots
    global careerSlots
    global dirtyFlag
    if openProfilePath:
        if activeList == 1:
            carSlots = myCarsSlots
            selSlot = selectedMyCarsSlot
        if activeList == 2:
            carSlots = careerSlots
            selSlot = selectedCareerSlot

        if selSlot <= 0:
            return
            
        slotMove = carSlots.pop(selSlot)
        slotNewPos = selSlot - 1
        carSlots.insert(slotNewPos, slotMove)
        if activeList == 1:
            for i in range(20):
                if carSlots[i][2][0:4] == b'\x00\x00\x00\x00':
                    i = i+1
                else:
                    carSlots[i][2] = bytearray(carSlots[i][2])
                    if i < 9:
                        carSlots[i][2][0:4] = f"0{i+1}MC".encode('ascii')
                        carSlots[i][2][4:8] = f"MC0{i+1}".encode('ascii')
                    else:
                        carSlots[i][2][0:4] = f"{i+1}MC".encode('ascii')
                        carSlots[i][2][4:8] = f"MC{i+1}".encode('ascii')
                    carSlots[i][2] = bytes(carSlots[i][2])
            myCarsListbox.selection_clear(selSlot)
            myCarsListbox.selection_set(slotNewPos)
        if activeList == 2:
            for i in range(5):
                if carSlots[i][2][0:4] == b'\x00\x00\x00\x00':
                    i = i+1
                else:
                    carSlots[i][2] = bytearray(carSlots[i][2])
                    carSlots[i][2][0:4] = f"0{i+1}CR".encode('ascii')
                    carSlots[i][2][4:8] = f"CR0{i+1}".encode('ascii')
                    carSlots[i][2] = bytes(carSlots[i][2])
            careerListbox.selection_clear(selSlot)
            careerListbox.selection_set(slotNewPos)
        myCarsListboxPopulate()
        careerListboxPopulate()
        loadSlots()
        
        dirtyFlag = 1

## moves slot down
def moveSlotDown(*args):
    global myCarsSlots
    global careerSlots
    global dirtyFlag
    if openProfilePath:
        if activeList == 1:
            carSlots = myCarsSlots
            selSlot = selectedMyCarsSlot
            if selSlot == 19:
                return
        if activeList == 2:
            carSlots = careerSlots
            selSlot = selectedCareerSlot
            if selSlot == 4:
                return
            
        slotMove = carSlots.pop(selSlot)
        slotNewPos = selSlot + 1
        carSlots.insert(slotNewPos, slotMove)
        
        if activeList == 1:
            for i in range(20):
                if carSlots[i][2][0:4] == b'\x00\x00\x00\x00':
                    i = i+1
                else:
                    carSlots[i][2] = bytearray(carSlots[i][2])
                    if i < 9:
                        carSlots[i][2][0:4] = f"0{i+1}MC".encode('ascii')
                        carSlots[i][2][4:8] = f"MC0{i+1}".encode('ascii')
                    else:
                        carSlots[i][2][0:4] = f"{i+1}MC".encode('ascii')
                        carSlots[i][2][4:8] = f"MC{i+1}".encode('ascii')
                    carSlots[i][2] = bytes(carSlots[i][2])
            myCarsListbox.selection_clear(selSlot)
            myCarsListbox.selection_set(slotNewPos)
        if activeList == 2:
            if selSlot == 4:
                return
            for i in range(5):
                if carSlots[i][2][0:4] == b'\x00\x00\x00\x00':
                    i = i+1
                else:
                    carSlots[i][2] = bytearray(carSlots[i][2])
                    carSlots[i][2][0:4] = f"0{i+1}CR".encode('ascii')
                    carSlots[i][2][4:8] = f"CR0{i+1}".encode('ascii')
                    carSlots[i][2] = bytes(carSlots[i][2])
            careerListbox.selection_clear(selSlot)
            careerListbox.selection_set(slotNewPos)
        myCarsListboxPopulate()
        careerListboxPopulate()
        loadSlots()
        
        dirtyFlag = 1

## exports slot data to a Binary-compatible preset file (.bin)
def exportPreset(*args):
    global myCarsSlots
    global careerSlots
    global exportPresetDir
    if openProfilePath:
        sponsorFlag = tk.IntVar(value=0)
        spPerfFlag = tk.IntVar(value=0)
        presetName = tk.StringVar(value='')
        
        if activeList == 1:
            exportSlots = myCarsSlots
            selSlot = selectedMyCarsSlot
        if activeList == 2:
            exportSlots = careerSlots
            selSlot = selectedCareerSlot

    ##  dialog to set up parameters like sponsor car flag and performance level
    def exportPresetSettings():
        def exportOkToggle(*args):
            if not presetName.get():
                exportOkBtn.state(['disabled'])
                exportPresetTop.unbind('<Return>')
            else:
                exportOkBtn.state(['!disabled'])
                exportPresetTop.bind('<Return>', exportOk)
                
        def exportOk(*args):
            with open (presetSave, 'wb') as presetWrite:
                presetWrite.write(b'\x00'*76)
                if sponsorFlag.get() != 0:
                    presetWrite.seek(0)
                    presetWrite.write(b'\x40\x14\x43')
                else:
                    pass
                presetWrite.seek(8)
                presetWrite.write(exportSlots[selSlot][0].encode('ascii'))
                presetWrite.seek(40)
                presetWrite.write(presetName.get().upper().encode('ascii'))
                presetWrite.seek(72)
                presetWrite.write(struct.pack('B', spPerfFlag.get()))
                presetWrite.seek(76)
                presetWrite.write(exportSlots[selSlot][2][28:776])
                slotPresetNameHash(exportSlots[selSlot][2][28:776], presetName.get().upper())
            presetWrite.close()
            exportPresetTop.destroy()
            if len(f'Slot {selSlot+1} exported to: {presetSave}') < 72:
                filePathStr.set(f'Slot {selSlot+1} exported to: {presetSave}')
            else:
                longpath1= os.path.splitdrive(presetSave)
                longpath2= os.path.split(longpath1[1])
                longpath3= os.path.split(longpath2[0])
                longpath4= os.path.split(longpath3[0])
                filePathStr.set(f'Slot {selSlot+1} exported to: {longpath1[0]}\\...\\{longpath3[1]}\\{longpath2[1]}')
            fileLabel.after(5000, openFileLabel)
            userDirPaths["exportPresetDir"] = os.path.split(presetSave)[0]
            myCarsListboxPopulate()
            careerListboxPopulate()
            loadSlots()
            saveUserDirPaths()

        def exportCancel(*args):
            exportPresetTop.destroy()
            return
                    
        exportDefaultName = os.path.splitext(os.path.basename(presetSave))[0][:32]
        presetName.set(exportDefaultName)
        exportPresetTop = tk.Toplevel(padx='5', pady='5')
        exportPresetTop.focus_force()
        exportPresetTop.grab_set()
        exportPresetTop.title("Export slot as preset")
        exportPresetTop.resizable(False,False)
        if os.name == "nt":
            exportPresetTop.attributes('-toolwindow',1)
        exportPresetNameLbl = ttk.Label(exportPresetTop, text="Enter preset name (up to 32 characters)")
        exportPresetNameLbl.grid(row = 0, column = 0, sticky="W")
        exportPresetNameEnt = ttk.Entry(exportPresetTop, width=50, textvariable=presetName)
        exportPresetNameEnt.grid(row = 1, column = 0, columnspan=2, sticky="WE")
        exportPresetNameEnt.focus_set()
        exportPreIsSponsorChk = ttk.Checkbutton(exportPresetTop, text="Sponsor car", variable=sponsorFlag)
        exportPreIsSponsorChk.grid(row = 2, column = 0, sticky="W")
        exportPreSpPerfLbl = ttk.Label(exportPresetTop, text="Performance level")
        exportPreSpPerfLbl.grid(row = 3, column = 0, sticky="WE")
        exportPreSpPerfLvl0Rd = ttk.Radiobutton(exportPresetTop, text="Stock", variable=spPerfFlag, value=0)
        exportPreSpPerfLvl0Rd.grid(row = 4, column = 0, sticky="WE")
        exportPreSpPerfLvl1Rd = ttk.Radiobutton(exportPresetTop, text="Level 1", variable=spPerfFlag, value=1)
        exportPreSpPerfLvl1Rd.grid(row = 5, column = 0, sticky="WE")
        exportPreSpPerfLvl2Rd = ttk.Radiobutton(exportPresetTop, text="Level 2", variable=spPerfFlag, value=2)
        exportPreSpPerfLvl2Rd.grid(row = 6, column = 0, sticky="WE")
        exportPreSpPerfLvl3Rd = ttk.Radiobutton(exportPresetTop, text="Level 3", variable=spPerfFlag, value=3)
        exportPreSpPerfLvl3Rd.grid(row = 7, column = 0, sticky="WE")
        exportOkBtn = ttk.Button(exportPresetTop, text="OK", command=exportOk, state='disabled')
        exportOkBtn.grid(row = 3, column = 1, sticky="WE")
        exportCancelBtn = ttk.Button(exportPresetTop, text="Cancel", command=exportPresetTop.destroy)
        exportCancelBtn.grid(row = 4, column = 1, sticky="WE")

        exportPresetTop.bind('<Escape>', exportCancel)
        exportPresetTop.bind('<KeyRelease>', exportOkToggle)

        regExportP = exportPresetTop.register(inputCallback)
        exportPresetNameEnt.config(validate='key', validatecommand=(regExportP, '%S', '%P')) 

    presetSave = filedialog.asksaveasfilename(title="Export preset", filetypes=[("NFSU2 Binary Preset", "*.bin *.BIN")], defaultextension=[".bin"], initialdir=userDirPaths["exportPresetDir"])
    if presetSave == "":
        return
    if not exportSlots[selSlot][0] in xnames.keys() and not exportSlots[selSlot][0] in userXnames.keys():
        addXnameDlg()
    exportPresetSettings()

## imports preset data from Binary-compatible preset file (.bin)
def importPreset(*args):
    global dirtyFlag
    global newXname
    global newXnameHash
    global myCarsSlots
    global careerSlots
    global presetImportFlag
    global importPresetDir

    importPerfLevel = 0
    
    perfLevels = [bytearray(b'\x00'*68),
                  bytearray(b'\x01\x01\x00\x00\x00\x00\x01\x01\x01\x00\x00\x00\x01\x00\x00\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x00\x00\x00\x00\x01\x00\x00\x01\x01\x01\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00'),
                  bytearray(b'\x01\x01\x01\x01\x00\x00\x01\x01\x00\x01\x01\x00\x00\x01\x00\x00\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x01\x00\x00\x01\x01\x01\x00\x00\x00\x01\x00\x00\x01\x00\x01\x01\x00\x00\x01\x01\x01\x01\x01\x01\x00\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00'),
                  bytearray(b'\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\x01\x01\x01\x01\x01\x01\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x01\x01\x01\x00\x01\x00\x00\x01\x01\x01\x01\x00\x01\x01\x01\x01\x01\x01\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00\x01\x00\x00\x01')]

    ## dialog to set performance level of imported preset
    def importPresetPerfLv(*args):
        spPerfFlag = tk.IntVar(value=0)
        
        def importPresetPerfLvOk(*args):
            global importPerfLvCnc
            importSlots[selSlot][2] = bytearray(importSlots[selSlot][2])
            if spPerfFlag.get() == 0:
                importSlots[selSlot][2][788:856] = perfLevels[0]
            elif spPerfFlag.get() == 1:
                importSlots[selSlot][2][788:856] = perfLevels[1]
            elif spPerfFlag.get() == 2:
                importSlots[selSlot][2][788:856] = perfLevels[2]
            elif spPerfFlag.get() == 3:
                importSlots[selSlot][2][788:856] = perfLevels[3]
            elif spPerfFlag.get() == -2:
                pass
            else:
                if importPerfLevel == 0:
                    importSlots[selSlot][2][788:856] = perfLevels[0]
                elif importPerfLevel == 1:
                    importSlots[selSlot][2][788:856] = perfLevels[1]
                elif importPerfLevel == 2:
                    importSlots[selSlot][2][788:856] = perfLevels[2]
                else:
                    importSlots[selSlot][2][788:856] = perfLevels[3]
            importSlots[selSlot][2] = bytes(importSlots[selSlot][2])
            importPerfLvCnc = 0
            importPresetPerfLvTop.destroy()

        def importPresetPerfLvCancel(*args):
            global importPerfLvCnc
            importPerfLvCnc = 1
            importPresetPerfLvTop.destroy()
            return
        
        importPresetPerfLvTop = tk.Toplevel(padx='5', pady='5')
        importPresetPerfLvTop.focus_force()
        importPresetPerfLvTop.grab_set()
        importPresetPerfLvTop.title("Import preset to slot")
        importPresetPerfLvTop.resizable(False,False)
        if os.name == "nt":
            importPresetPerfLvTop.attributes('-toolwindow',1)
            
        importPresetPerfLvLbl = ttk.Label(importPresetPerfLvTop, text="Select performance level of imported preset")
        importPresetPerfLvLbl.grid(row = 0, column = 0, sticky="W")
        if importPerfLevel == 0:
            importAutoString = "Stock"
        elif importPerfLevel == 1:
            importAutoString = "Level 1"
        elif importPerfLevel == 2:
            importAutoString = "Level 2"
        else:
            importAutoString = "Level 3"
        importPresetPerfLvAutoRd = ttk.Radiobutton(importPresetPerfLvTop, text=f"Detect automatically ({importAutoString})", variable=spPerfFlag, value=-1)
        importPresetPerfLvAutoRd.grid(row = 1, column = 0, sticky="WE")
        importPresetPerfLvAutoRd.invoke()
        importPresetPerfLvSkipRd = ttk.Radiobutton(importPresetPerfLvTop, text="Don't set performance data", variable=spPerfFlag, value=-2)
        importPresetPerfLvSkipRd.grid(row = 2, column = 0, sticky="WE")
        importPresetPerfLvl0Rd = ttk.Radiobutton(importPresetPerfLvTop, text="Stock", variable=spPerfFlag, value=0)
        importPresetPerfLvl0Rd.grid(row = 3, column = 0, sticky="WE")
        importPresetPerfLvl1Rd = ttk.Radiobutton(importPresetPerfLvTop, text="Level 1", variable=spPerfFlag, value=1)
        importPresetPerfLvl1Rd.grid(row = 4, column = 0, sticky="WE")
        importPresetPerfLvl2Rd = ttk.Radiobutton(importPresetPerfLvTop, text="Level 2", variable=spPerfFlag, value=2)
        importPresetPerfLvl2Rd.grid(row = 5, column = 0, sticky="WE")
        importPresetPerfLvl3Rd = ttk.Radiobutton(importPresetPerfLvTop, text="Level 3", variable=spPerfFlag, value=3)
        importPresetPerfLvl3Rd.grid(row = 6, column = 0, sticky="WE")

        importPresetPerfLvOkBtn = ttk.Button(importPresetPerfLvTop, text="OK", command=importPresetPerfLvOk)
        importPresetPerfLvOkBtn.grid(row = 1, column = 1, sticky="WE")
        importPresetPerfLvCancelBtn = ttk.Button(importPresetPerfLvTop, text="Cancel", command=importPresetPerfLvCancel)
        importPresetPerfLvCancelBtn.grid(row = 2, column = 1, sticky="WE")

        importPresetPerfLvTop.bind('<Return>', importPresetPerfLvOk)
        importPresetPerfLvTop.bind('<Escape>', importPresetPerfLvCancel)

        root.wait_window(importPresetPerfLvTop)
    
    if openProfilePath:
        if activeList == 1:
            importSlots = myCarsSlots
            selSlot = selectedMyCarsSlot
        if activeList == 2:
            importSlots = careerSlots
            selSlot = selectedCareerSlot
        
        presetOpen = filedialog.askopenfilename(title="Import preset", filetypes=[("NFSU2 Binary Preset", "*.bin *.BIN")], initialdir=userDirPaths["importPresetDir"])
        if presetOpen == "":
            return
        with open (presetOpen, 'rb') as presetRead:
            if 824 < len(presetRead.read()) > 825 :
                badFile=messagebox.showerror(title="Error", message="Invalid preset file, please select a valid unserialized preset file.")
                return
            presetRead.seek(8)
            presetXname=''
            for c in presetRead.read(32).decode('ascii'):
                if c.isalnum() or c == "_":
                    presetXname += c
                    
            if not presetXname in xnames.keys() and not presetXname in userXnames.keys():
                newXname.set(presetXname)
                presetImportFlag = 1
                addXnameDlg()
                presetXnameHash = newXnameHash.get()
                if presetXnameHash == '':
                    return
            else:
                if presetXname in xnames.keys():
                    presetXnameHash = xnames[presetXname]
                if presetXname in userXnames.keys():
                    presetXnameHash = userXnames[presetXname]
            presetRead.seek(76)
            presetData = presetRead.read(748)
            presetRead.seek(72)
            importPerfLevel = struct.unpack('>B',presetRead.read(1))[0]
            importPresetPerfLv()
            if importPerfLvCnc == 1:
                return
            presetRead.close()
            userDirPaths["importPresetDir"] = os.path.split(presetOpen)[0]
            saveUserDirPaths()
            
        newXname.set(presetXnameHash)
        importSlots[selSlot][0] = presetXname
        importSlots[selSlot][1] = struct.pack('>I',int(presetXnameHash,16))
        importSlots[selSlot][2] = bytearray(importSlots[selSlot][2])
        importSlots[selSlot][2][29:776] = presetData
        importSlots[selSlot][2][25:29] = importSlots[selSlot][1]
        slotPresetNameHash(presetData, presetOpen)
        if activeList == 1:
            if selSlot < 9:
                importSlots[selSlot][2][0:4] = f"0{selSlot+1}MC".encode('ascii')
                importSlots[selSlot][2][4:9] = f"MC0{selSlot+1}".encode('ascii')
            else:
                importSlots[selSlot][2][0:4] = f"{selSlot+1}MC".encode('ascii')
                importSlots[selSlot][2][4:9] = f"MC{selSlot+1}".encode('ascii')
            importSlots[selSlot][2][8:9] = struct.pack('>B',1)
            importSlots[selSlot][2][12:13] = struct.pack('>B',2)
            importSlots[selSlot][2] = bytes(myCarsSlots[selectedMyCarsSlot][2])
        if activeList == 2:
            if selSlot < 9:
                importSlots[selSlot][2][0:4] = f"0{selSlot+1}CR".encode('ascii')
                importSlots[selSlot][2][4:9] = f"CR0{selSlot+1}".encode('ascii')
            else:
                importSlots[selSlot][2][0:4] = f"{selSlot+1}CR".encode('ascii')
                importSlots[selSlot][2][4:9] = f"CR{selSlot+1}".encode('ascii')
            importSlots[selSlot][2][8:9] = struct.pack('>B',1)
            importSlots[selSlot][2][12:13] = struct.pack('>B',4)
            importSlots[selSlot][2][1069:1070] = struct.pack('>B',1)
            importSlots[selSlot][2] = bytes(importSlots[selSlot][2])
            
        dirtyFlag = 1
        newXname.set('')
        newXnameHash.set('')
        myCarsListboxPopulate()
        careerListboxPopulate()
        loadSlots()
        if len(f'Imported {presetOpen} to slot {selSlot+1}') < 72:
            filePathStr.set(f'Imported {presetOpen} to slot {selSlot+1}')
        else:
            longpath1= os.path.splitdrive(presetOpen)
            longpath2= os.path.split(longpath1[1])
            longpath3= os.path.split(longpath2[0])
            longpath4= os.path.split(longpath3[0])
            filePathStr.set(f'Imported {longpath1[0]}\\...\\{longpath3[1]}\\{longpath2[1]} to slot {selSlot+1}')
        fileLabel.after(5000, openFileLabel)


## opens Add XNAME dialog on button command
def addXnameSolo(*args):
    global activeList
    activeListPrev = activeList
    if openProfilePath == '':
        activeList = 0        
    addXnameDlg()
    activeList = activeListPrev

## opens Add XNAME dialog when called, also auto fills XNAME or hash when found
def addXnameDlg():
    global myCarsSlots
    global careerSlots
    global userXnames
    global newXname
    global newXnameHash
    global presetImportFlag
    
    if presetImportFlag == 0:
        newXname.set('')
        newXnameHash.set('')
        
    if activeList == 1:
        carSlots = myCarsSlots
        selSlot = selectedMyCarsSlot
    if activeList == 2:
        carSlots = careerSlots
        selSlot = selectedCareerSlot

    ## gets hash from a string, thanks to TerminatorVasya for lending me his code
    def hashString(string):
        if string is None:
            return 0
        
        result = -1

        for char in string:
            result = result * 0x21 + ord(char)
        # Mask the result to keep only the last 4 bytes
        result &= 0xFFFFFFFF

        # Convert to bytes in little-endian order (reverse byte order)
        reversed_bytes = result.to_bytes(4, byteorder='big')[::-1]

        # Convert back to hexadecimal string
        return ''.join(f"{byte:02X}" for byte in reversed_bytes)

    ## callback function for XNAME entry field, calls hashString and fills hash entry field on each key release 
    def generateHashString(*args):
        if newXname.get() != '':
            newXnameHashRaw = hashString(newXname.get())
            newXnameHashSet = f"{int(newXnameHashRaw,16):#0{10}x}"
            newXnameHash.set(newXnameHashSet.upper().replace("0X","0x"))
            return newXnameHashSet

    def addXnameOkToggle(*args):
        if not newXname.get():
            addXnameOkBtn.state(['disabled'])
            addXnameTop.unbind('<Return>')
        elif not newXnameHash.get():
            addXnameOkBtn.state(['disabled'])
        else:
            addXnameOkBtn.state(['!disabled'])
            addXnameTop.bind('<Return>', addXnameOk)
    
    def addXnameOk(*args):
        global presetImportFlag
        
        with open ("userXnames.txt", 'w') as userXnamesFile:
            newXnameHashWrite = f"{int(newXnameHash.get(),16):#0{10}x}"
            userXnames[newXname.get().upper()] = str(newXnameHashWrite).upper().replace("0X","0x")
            userXnamesFile.write(json.dumps(userXnames, indent=4))
            userXnamesFile.close
        loadUserXnames()
        if openProfilePath:
            carSlots[selSlot][0] = newXname.get().upper()
            myCarsListboxPopulate()
            careerListboxPopulate()
        if presetImportFlag == 0:
            newXname.set('')
            newXnameHash.set('')
        else:
            presetImportFlag = 0
        addXnameTop.destroy()

    def addXnameCancel(*args):
        global presetImportFlag
        newXname.set('')
        newXnameHash.set('')
        presetImportFlag = 0
        addXnameTop.destroy()
        return

    addXnameTop = tk.Toplevel(padx='5', pady='5')
    addXnameTop.title("Add car XNAME and hash")
    addXnameTop.resizable(False,False)
    if os.name == "nt":
        addXnameTop.attributes('-toolwindow',1)
    addXnameTop.minsize(350,150)
    addXnameTop.focus_force()
    addXnameTop.grab_set()
    addXnameMsgLbl = ttk.Label(addXnameTop, text="Please enter the XNAME and hash to add.\n")
    if activeList == 0:
        addXnameMsgLbl = ttk.Label(addXnameTop, text="Please enter the XNAME and hash to add.\n")
    elif newXname.get() != '' and not newXname.get() in xnames.keys() and not newXname.get() in userXnames.keys() and activeList != 0:
        addXnameMsgLbl = ttk.Label(addXnameTop, text="Car XNAME does not exist in current lists, please add them.\nEnter the XNAME and hash to add.\n")
    elif not carSlots[selSlot][0] in xnames.keys() and not carSlots[selSlot][0] in userXnames.keys() and activeList != 0:
        addXnameMsgLbl = ttk.Label(addXnameTop, text="Car XNAME does not exist in current lists, please add them.\nEnter the XNAME and hash to add.\n")
        newXnameHash.set(carSlots[selSlot][0])
    addXnameMsgLbl.grid(row=0, column=0, columnspan=3, sticky='NSEW')
    addXnameLabel = ttk.Label(addXnameTop, text="XNAME")
    addXnameLabel.grid(row=1, column=0, columnspan=3, sticky='NSEW')
    addXnameEntry = ttk.Entry(addXnameTop, textvariable=newXname)
    addXnameEntry.grid(row=2, column=0, columnspan=3, sticky='NSEW', pady='5')
    addXnameEntry.focus_set()
    addXnameHashLbl = ttk.Label(addXnameTop, text="Hash")
    addXnameHashLbl.grid(row=3, column=0, columnspan=3, sticky='NSEW')
    addXnameHashEnt = ttk.Entry(addXnameTop, textvariable=newXnameHash)
    addXnameHashEnt.grid(row=4, column=0, columnspan=3, sticky='NSEW')
    addXnameOkBtn = ttk.Button(addXnameTop, text="OK", command=addXnameOk, state='disabled')
    addXnameOkBtn.grid(row=5, column=1, pady='5', sticky='E')
    addXnameCancBtn = ttk.Button(addXnameTop, text="Cancel", command=addXnameCancel)
    addXnameCancBtn.grid(row=5, column=2, pady='5', sticky='E')

    if presetImportFlag == 1:
        generateHashString()
        addXnameOkToggle()

    addXnameTop.columnconfigure(0, weight=1)

    regAddXname = addXnameTop.register(inputCallback)
    regAddXnameHex = addXnameTop.register(inputCallbackHex)
    addXnameEntry.config(validate='key', validatecommand=(regAddXname, '%S','%P'))
    addXnameHashEnt.config(validate='key', validatecommand=(regAddXnameHex, '%S','%P'))

    addXnameTop.bind('<Escape>', addXnameCancel)
    addXnameTop.bind('<KeyRelease>', addXnameOkToggle)
    addXnameEntry.bind('<KeyRelease>', generateHashString)
        
    root.wait_window(addXnameTop)

## opens up serialized preset file to change its XNAME
def changeSerPresetXname(*args):
    global xnames
    global userXnames
    global newXname

    openSerPresetFile = filedialog.askopenfilename(title="Open your serialized preset", filetypes=[("NFSU2 Binary preset", "*.bin *.BIN")])
    if openSerPresetFile == '':
            return
    
    with open (openSerPresetFile, "rb") as preset:
        if preset.read(3) != b'gMp':
            badFile = messagebox.showerror(title="Error", message="Not a serialized Binary preset .bin file")
            return
        openSerPreset=preset.read()
        preset.seek(28)
        filesizeData=struct.unpack('h',preset.read(2))[0]
        xnamesKeys = list(xnames.keys())
        userXnamesKeys = list(userXnames.keys())
        for i in range(len(xnamesKeys)):
            if str(openSerPreset).find(f"{xnamesKeys[i]}_BASE") != -1:
                 xname = f"{xnamesKeys[i]}".encode('ascii')
            elif len(xnamesKeys)-i >= 0:
                pass
            elif len(xnamesKeys)-i <= 0:
                if str(openSerPreset).find(f"{userXnamesKeys[i]}_BASE") != -1:
                    xname = f"{userXnamesKeys[i]}".encode('ascii')
            else:
                addXnameDlg()
                xname = newXname.get()

    def chgSerPreXnameOkToggle(*args):
        if not newXname.get():
            chgSerPreXnameOkBtn.state(['disabled'])
            chgSerPreXnameTop.unbind('<Return>')
        else:
            chgSerPreXnameOkBtn.state(['!disabled'])
            chgSerPreXnameTop.bind('<Return>', chgSerPreXnameOk)
            
    def chgSerPreXnameOk(*args):
        with open (openSerPresetFile, "rb") as preset:
            openSerPreset=preset.read()
            preset.seek(0)
            openSerPreset=openSerPreset.replace(xname, newXname.get().encode('ascii'))
            savePreset = filedialog.asksaveasfilename(title="Save NFSU2 serialized preset as...", filetypes=[("NFSU2 Binary preset", "*.bin*")], defaultextension=[".bin"])
            
            if savePreset == "":
                pass
            with open (savePreset, 'wb') as presetWrite:
                presetWrite.write(openSerPreset)
                if len(xname) < len(newXname.get()):
                    preset.seek(28)
                    filesizeDataNew=struct.pack('h',filesizeData+200)
                    presetWrite.seek(28)
                    presetWrite.write(filesizeDataNew)

            presetWrite.close()                
            chgSerPreXnameTop.destroy()

    def chgSerPreXnameCancel(*args):
        newXname.set('')
        chgSerPreXnameTop.destroy()
        return

    chgSerPreXnameTop = tk.Toplevel(padx='5', pady='5')
    chgSerPreXnameTop.title("Change serialized preset XNAME")
    chgSerPreXnameTop.resizable(False,False)
    if os.name == "nt":
        chgSerPreXnameTop.attributes('-toolwindow',1)
    chgSerPreXnameTop.minsize(300,100)
    chgSerPreXnameTop.focus_force()
    chgSerPreXnameTop.grab_set()
    chgSerPreXnameCurLabel = ttk.Label(chgSerPreXnameTop, text=f'Current XNAME: {xname.decode("ascii")}')
    chgSerPreXnameCurLabel.grid(row=0, column=0, columnspan=3, sticky='NSEW')
    chgSerPreXnameSpacerLbl = ttk.Label(chgSerPreXnameTop, text="")
    chgSerPreXnameSpacerLbl.grid(row=1, column=0, sticky="NSEW", pady='5')
    chgSerPreXnameNewLbl = ttk.Label(chgSerPreXnameTop, text="New XNAME:")
    chgSerPreXnameNewLbl.grid(row=2, column=0, columnspan=3, sticky='NSEW')
    chgSerPreXnameNewEnt = ttk.Entry(chgSerPreXnameTop, textvariable=newXname)
    chgSerPreXnameNewEnt.grid(row=3, column=0, columnspan=3, sticky='NSEW')
    chgSerPreXnameNewEnt.focus_set()
    chgSerPreXnameOkBtn = ttk.Button(chgSerPreXnameTop, text="OK", command=chgSerPreXnameOk, state='disabled')
    chgSerPreXnameOkBtn.grid(row=4, column=1, pady='5', sticky='E')
    chgSerPreXnameCancBtn = ttk.Button(chgSerPreXnameTop, text="Cancel", command=chgSerPreXnameCancel)
    chgSerPreXnameCancBtn.grid(row=4, column=2, pady='5', sticky='E')

    chgSerPreXnameTop.columnconfigure(0, weight=1)

    regChgXname = chgSerPreXnameTop.register(inputCallback)
    chgSerPreXnameNewEnt.config(validate='key', validatecommand=(regChgXname, '%S','%P'))

    chgSerPreXnameTop.bind('<Escape>', chgSerPreXnameCancel)
    chgSerPreXnameTop.bind('<KeyRelease>', chgSerPreXnameOkToggle)
        
    root.wait_window(chgSerPreXnameTop)

## hacky keybind to open the Tools menu
def toolsMenuKbind(event):
    toolsMenuBtn.menu.post(toolsMenuBtn.winfo_rootx(),
                           toolsMenuBtn.winfo_rooty() + toolsMenuBtn.winfo_height())
    
    

## defines window widgets
openProfileBtn = ttk.Button(topFrame, image=openBtnIcon, text='Open', style='TopButton.TButton', command=openProfile)
openProfileBtn.grid(row = 0, column=0, sticky='W')
openTip = Hovertip(openProfileBtn, 'Open profile (Ctrl+O)')
saveProfileBtn = ttk.Button(topFrame, image=saveBtnIcon, text='Save', state='disabled', command=saveProfile)
saveProfileBtn.grid(row = 0, column=1, sticky='W')
saveTip = Hovertip(saveProfileBtn, 'Save profile (Ctrl+S)')
saveAsProfileBtn = ttk.Button(topFrame, image=saveAsBtnIcon, text='Save As...', state='disabled', command=saveProfileAs)
saveAsProfileBtn.grid(row = 0, column=2, sticky='W')
saveAsTip = Hovertip(saveAsProfileBtn, 'Save profile as... (Ctrl+Shift+S)')
reloadProfileBtn = ttk.Button(topFrame, image=reloadBtnIcon, text='Reload Profile', state='disabled', command=reloadProfile)
reloadProfileBtn.grid(row = 0, column=3, sticky='W')
reloadTip = Hovertip(reloadProfileBtn, 'Reload profile (Ctrl+R)')
topSpacerLbl = ttk.Label(topFrame, text='')
topSpacerLbl.grid(row = 0, column = 3, sticky = 'NSEW', padx=100)
toolsMenuBtn = ttk.Menubutton(topFrame, image=toolsBtnIcon, text='Tools...', takefocus=True)
toolsMenuBtn.grid(row = 0, column=4, sticky="E")
toolsMenuBtnTip = Hovertip(toolsMenuBtn, 'Tools (Alt+T)')
toolsMenuBtn.menu = tk.Menu(toolsMenuBtn, tearoff=0)
toolsMenuBtn["menu"] = toolsMenuBtn.menu
toolsMenuBtn.menu.add_command(
    label="Add XNAME...",
    accelerator="Ctrl-A",
    image=addXnameBtnIcon,
    compound=tk.LEFT,
    command=addXnameSolo
    )
toolsMenuBtn.menu.add_command(
    label="Change XNAME in serialized preset file...",
    accelerator="Ctrl-Shift-C",
    image=chgXnameBtnIcon,
    compound=tk.LEFT,
    command=changeSerPresetXname
    )
toolsMenuBtn.menu.add_separator()
toolsMenuBtn.menu.add_command(
    label="About...",
    accelerator="F1",
    image=aboutBtnIcon,
    compound=tk.LEFT,
    command=aboutDlg
    )

myCarsListbox = tk.Listbox(myCarsTabLeft, listvariable=myCarsSlotsListVar, exportselection=False, state='disabled')
myCarsListbox.grid(row=0, column=0, columnspan=2, rowspan=15, sticky="NSEW")
myCarsListbox.bind('<<ListboxSelect>>', loadSlots)
myCarsLbScroll = ttk.Scrollbar(myCarsTabLeft, orient=tk.VERTICAL, command=myCarsListbox.yview)
myCarsLbScroll.grid(row=0, column=2, sticky="NSEW")
myCarsListbox.configure(yscrollcommand = myCarsLbScroll.set)
myCarsLbHScroll = ttk.Scrollbar(myCarsTabLeft, orient=tk.HORIZONTAL, command=myCarsListbox.xview)
myCarsLbHScroll.grid(row=1, column=0, sticky="NSEW")
myCarsListbox.configure(xscrollcommand = myCarsLbHScroll.set)
exportMyCarsSlotBtn = ttk.Button(myCarsTabRight, text="Export slot", image=slotExportIcon, compound='left', command=exportSlot, state='disabled', style='TabButton.TButton')
exportMyCarsSlotBtn.grid(row=0, column=0, sticky="NSEW")
exportMyCarsTip = Hovertip(exportMyCarsSlotBtn, 'Export slot (Ctrl+E)')
importMyCarsSlotBtn = ttk.Button(myCarsTabRight, text="Import slot", image=slotImportIcon, compound='left', command=importSlot, state='disabled', style='TabButton.TButton')
importMyCarsSlotBtn.grid(row=1, column=0, sticky="NSEW")
importMyCarsSlotTip = Hovertip(importMyCarsSlotBtn, 'Import slot (Ctrl+I)')
clearMyCarsSlotBtn = ttk.Button(myCarsTabRight, text="Clear slot", image=slotClearIcon, compound='left', command=clearSlot, state='disabled', style='TabButton.TButton')
clearMyCarsSlotBtn.grid(row=2, column=0, sticky="NSEW")
clearMyCarsSlotTip = Hovertip(clearMyCarsSlotBtn, 'Clear slot (Ctrl+Delete)')
myCarsSpacerLbl = ttk.Label(myCarsTabRight, text="")
myCarsSpacerLbl.grid(row=3, column=0, sticky="NSEW", pady='5')
myCarsMoveSlotUpBtn = ttk.Button(myCarsTabRight, text="Move slot up", image=moveSlotUpIcon, compound='left', command=moveSlotUp, state='disabled', style='TabButton.TButton')
myCarsMoveSlotUpBtn.grid(row=4, column=0, sticky="NSEW")
myCarsMoveSlotUpTip = Hovertip(myCarsMoveSlotUpBtn, 'Move slot up (Ctrl+Page Up)')
myCarsMoveSlotDownBtn = ttk.Button(myCarsTabRight, text="Move slot down", image=moveSlotDownIcon, compound='left', command=moveSlotDown, state='disabled', style='TabButton.TButton')
myCarsMoveSlotDownBtn.grid(row=5, column=0, sticky="NSEW")
myCarsMoveSlotDownTip = Hovertip(myCarsMoveSlotDownBtn, 'Move slot down (Ctrl+Page Down)')
myCarsSpacerLbl2 = ttk.Label(myCarsTabRight, text="")
myCarsSpacerLbl2.grid(row=6, column=0, sticky="NSEW", pady='5')
exportMyCarsPsetBtn = ttk.Button(myCarsTabRight, text="Export as preset", image=exportPresetIcon, compound='left', command=exportPreset, state='disabled', style='TabButton.TButton')
exportMyCarsPsetBtn.grid(row=7, column=0, sticky="NSEW")
exportMyCarsPsetTip = Hovertip(exportMyCarsPsetBtn, 'Export as preset (Ctrl+Shift+E)')
importMyCarsPsetBtn = ttk.Button(myCarsTabRight, text="Import preset", image=importPresetIcon, compound='left', command=importPreset, state='disabled', style='TabButton.TButton')
importMyCarsPsetBtn.grid(row=8, column=0, sticky="NSEW")
importMyCarsPsetTip = Hovertip(importMyCarsPsetBtn, 'Import preset (Ctrl+Shift+I)')

careerListbox = tk.Listbox(careerTabLeft, listvariable=careerSlotsListVar, exportselection=False, state='disabled')
careerListbox.grid(row=0, column=0, columnspan=2, rowspan=15, sticky="NSEW")
careerListbox.bind('<<ListboxSelect>>', loadSlots)
careerLbScroll = ttk.Scrollbar(careerTabLeft, orient=tk.VERTICAL, command=careerListbox.yview)
careerLbScroll.grid(row=0, column=2, sticky="NSEW")
careerListbox.configure(yscrollcommand = careerLbScroll.set)
careerLbHScroll = ttk.Scrollbar(careerTabLeft, orient=tk.HORIZONTAL, command=careerListbox.xview)
careerLbHScroll.grid(row=1, column=0, sticky="NSEW")
careerListbox.configure(xscrollcommand = careerLbHScroll.set)
exportCareerSlotBtn = ttk.Button(careerTabRight, text="Export slot", image=slotExportIcon, compound='left', command=exportSlot, state='disabled', style='TabButton.TButton')
exportCareerSlotBtn.grid(row=0, column=0, sticky="NSEW")
exportCareerSlotTip = Hovertip(exportCareerSlotBtn, 'Export slot (Ctrl+E)')
importCareerSlotBtn = ttk.Button(careerTabRight, text="Import slot", image=slotImportIcon, compound='left', command=importSlot, state='disabled', style='TabButton.TButton')
importCareerSlotBtn.grid(row=1, column=0, sticky="NSEW")
importCareerSlotTip = Hovertip(importCareerSlotBtn, 'Import slot (Ctrl+I)')
clearCareerSlotBtn = ttk.Button(careerTabRight, text="Clear slot", image=slotClearIcon, compound='left', command=clearSlot, state='disabled', style='TabButton.TButton')
clearCareerSlotBtn.grid(row=2, column=0, sticky="NSEW")
clearCareerSlotTip = Hovertip(clearCareerSlotBtn, 'Clear slot (Ctrl+Delete)')
careerSpacerLbl = ttk.Label(careerTabRight, text="")
careerSpacerLbl.grid(row=3, column=0, sticky="NSEW", pady='5')
careerMoveSlotUpBtn = ttk.Button(careerTabRight, text="Move slot up", image=moveSlotUpIcon, compound='left', command=moveSlotUp, state='disabled', style='TabButton.TButton')
careerMoveSlotUpBtn.grid(row=4, column=0, sticky="NSEW")
careerMoveSlotUpTip = Hovertip(careerMoveSlotUpBtn, 'Move slot up (Ctrl+Page Up)')
careerMoveSlotDownBtn = ttk.Button(careerTabRight, text="Move slot down", image=moveSlotDownIcon, compound='left', command=moveSlotDown, state='disabled', style='TabButton.TButton')
careerMoveSlotDownBtn.grid(row=5, column=0, sticky="NSEW")
careerMoveSlotDownTip = Hovertip(careerMoveSlotDownBtn, 'Move slot down (Ctrl+Page Down)')
careerSpacerLbl2 = ttk.Label(careerTabRight, text="")
careerSpacerLbl2.grid(row=6, column=0, sticky="NSEW", pady='5')
exportCareerPsetBtn = ttk.Button(careerTabRight, text="Export as preset", image=exportPresetIcon, compound='left', command=exportPreset, state='disabled', style='TabButton.TButton')
exportCareerPsetBtn.grid(row=7, column=0, sticky="NSEW")
exportCareerPsetTip = Hovertip(exportCareerPsetBtn, 'Export as preset (Ctrl+Shift+E)')
importCareerPsetBtn = ttk.Button(careerTabRight, text="Import preset", image=importPresetIcon, compound='left', command=importPreset, state='disabled', style='TabButton.TButton')
importCareerPsetBtn.grid(row=8, column=0, sticky="NSEW")
importCareerPsetTip = Hovertip(importCareerPsetBtn, 'Import preset (Ctrl+Shift+I)')

footSeparator = ttk.Separator(bottomFrame, orient="horizontal")
footSeparator.grid(row = 0, column = 0, sticky="EW")
fileLabel = ttk.Label(bottomFrame, textvariable=filePathStr, width = 10)
fileLabel.grid(row = 1, column = 0, padx=2, pady=2, sticky='EW')

## sets active tab on tab change
mainNotebook.bind("<<NotebookTabChanged>>", activeTab)

## sets key bindings
root.bind('<Control-o>', openProfile)
root.bind('<Control-O>', openProfile)
root.bind('<Control-s>', saveProfile)
root.bind('<Control-S>', saveProfile)
root.bind('<Control-Shift-s>', saveProfileAs)
root.bind('<Control-Shift-S>', saveProfileAs)
root.bind('<Control-r>', reloadProfile)
root.bind('<Control-R>', reloadProfile)
root.bind('<Alt-t>', toolsMenuKbind)
root.bind('<Alt-T>', toolsMenuKbind)
root.bind('<Control-a>', addXnameSolo)
root.bind('<Control-A>', addXnameSolo)
root.bind('<Control-Shift-c>', changeSerPresetXname)
root.bind('<Control-Shift-C>', changeSerPresetXname)
root.bind('<F1>', aboutDlg)
root.bind('<F1>', aboutDlg)
root.bind('<Control-e>', exportSlot)
root.bind('<Control-E>', exportSlot)
root.bind('<Control-i>', importSlot)
root.bind('<Control-I>', importSlot)
root.bind('<Control-Delete>', clearSlot)
root.bind('<Control-Prior>', moveSlotUp)
root.bind('<Control-Next>', moveSlotDown)
root.bind('<Control-Shift-e>', exportPreset)
root.bind('<Control-Shift-E>', exportPreset)
root.bind('<Control-Shift-i>', importPreset)
root.bind('<Control-Shift-I>', importPreset)

root.mainloop()
